﻿package com.oyjd.ybx.business.msg.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.InformationVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.msg.service.MsgService;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;
import com.oyjd.ybx.pub.Const;

/**
 * 
 * 资讯信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/msg")
@Results({ @Result(name = "viewList", location = Const.JSP_msg_list), @Result(name = "viewAdd", location = Const.JSP_msg_add), @Result(name = "viewEdit", location = Const.JSP_msg_edit) })
public class MsgAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(MsgAction.class);
	private MsgService service;

	@Autowired
	public void setService(MsgService service) {
		this.service = service;
	}

	/**
	 * 进入资讯信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入资讯信息新增页面
	 * 
	 * @return
	 */
	public String viewAdd() {
		return "viewAdd";
	}

	/**
	 * 进入资讯信息修改页面
	 * 
	 * @return
	 */
	public String viewEdit() {
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			InformationVO vo = (InformationVO) service.getInformationVO(id);
			req.setAttribute("data", vo);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewEdit";
	}

	/**
	 * 获取资讯信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<InformationVO> page = service.getList(new Page<InformationVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增资讯信息
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			String msgTitle = ServletRequestUtils.getStringParameter(req, "msgTitle");
			String msgContent = ServletRequestUtils.getStringParameter(req, "msgContent");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(classId, msgTitle, msgContent, msgPicFileName, msgPic, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 编辑资讯信息
	 * 
	 * @return
	 */
	public String edit() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String msgId = ServletRequestUtils.getStringParameter(req, "msgId");
			String classId = ServletRequestUtils.getStringParameter(req, "classId");
			String msgTitle = ServletRequestUtils.getStringParameter(req, "msgTitle");
			String msgContent = ServletRequestUtils.getStringParameter(req, "msgContent");
			service.edit(msgId, classId, msgTitle, msgContent, msgPicFileName, msgPic);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除资讯信息
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String[] cbs = ServletRequestUtils.getStringParameters(req, "cb");
			service.remove(cbs);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	// 上传文件域对象
	private File msgPic;
	// 上传文件名
	private String msgPicFileName;

	public void setMsgPic(File msgPic) {
		this.msgPic = msgPic;
	}

	public void setMsgPicFileName(String msgPicFileName) {
		this.msgPicFileName = msgPicFileName;
	}

}
